// VBConversions Note: VB project level imports
using System.Collections.Generic;
using System;
using System.Linq;
using System.Drawing;
using System.Diagnostics;
using Microsoft.VisualBasic;
using System.Xml.Linq;
using Microsoft.VisualBasic.Compatibility;
using System.Windows.Forms;
using System.Collections;
using System.Data;
// End of VB project level imports

using System.Runtime.InteropServices;
using System.Drawing.Imaging;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;


namespace 喷码数据生成工具
{
	sealed class sAPI
	{
		public const int SC_MOVE = 0xF012;
		public const int SC_NCLBUTTONDOWN = 0xA1;
		public const int HTCAPTION = 2;
		public const int WM_SYSCOMMAND = 0x112;
		public const int WM_MOVING = 0x216;
		public const int WM_MOVE = 0x3;
		[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]public struct RECT
		{
			public int Left;
			public int Top;
			public int Right;
			public int Bottom;
		}
		//获得整个窗口的范围矩形，窗口的边框、标题栏、滚动条及菜单等都在这个矩形内
		[DllImport("user32", EntryPoint = "GetWindowRect", SetLastError = false, 
		CharSet = CharSet.Auto, ExactSpelling = false, 
		CallingConvention = CallingConvention.StdCall)]public static  extern int GetWindowRect(IntPtr hWnd, ref 
		RECT lpRect);
		
		[DllImport("user32.dll", EntryPoint = "SendMessage")]public static  extern int SendMessage(int hWnd, int wMsg, int wParam, int lParam);
		[DllImport("user32.dll", EntryPoint = "ReleaseCapture")]public static  extern int ReleaseCapture();
		//暂停线索
		[DllImport("kernel32", EntryPoint = "Sleep", SetLastError = false, 
		CharSet = CharSet.Auto, ExactSpelling = false, 
		CallingConvention = CallingConvention.StdCall)]public static  extern void Sleep(int dwMilliseconds);
		
		[DllImport("kernel32",EntryPoint="WritePrivateProfileStringA", ExactSpelling=true, CharSet=CharSet.Ansi, SetLastError=true)]
		public static extern int WritePrivateProfileString(string lpApplicationName, string lpKeyName, string lpString, string lpFileName);
		//为初始化文件中指定的条目取得字串
		[DllImport("kernel32",EntryPoint="GetPrivateProfileStringA", ExactSpelling=true, CharSet=CharSet.Ansi, SetLastError=true)]
		public static extern int GetPrivateProfileString(string lpApplicationName, string lpKeyName, string lpDefault, string lpReturnedString, int nSize, string lpFileName);
		//指定一个窗口的新父（在vb里使用：利用这个函数，vb可以多种形式支持子窗口。例如，可将控件从一个容器移至窗体中的另一个。用这个函数在窗体间移动控件是相当冒险的，但却不失为一个有效的办法。如真的这样做，请在关闭任何一个窗体之前，注意用SetParent将控件的父设回原来的那个）
		[DllImport("user32", EntryPoint = "SetParent", SetLastError = false, 
		CharSet = CharSet.Auto, ExactSpelling = false, 
		CallingConvention = CallingConvention.StdCall)]public static  extern int SetParent(int hWndChild, 
		int hWndNewParent);
		public enum ENUMMOUSEEVENT
		{
			/// <summary>
			/// 常规状态
			/// </summary>
			/// <remarks></remarks>
			MOUSENORMAL = 0,
			/// <summary>
			/// 鼠标经过
			/// </summary>
			/// <remarks></remarks>
			MOUSEHOVER = 1,
			/// <summary>
			/// 鼠标按下
			/// </summary>
			/// <remarks></remarks>
			MOUSEDOWN = 2,
			/// <summary>
			/// 鼠标放开
			/// </summary>
			/// <remarks></remarks>
			MOUSEUP = 3,
			/// <summary>
			/// 鼠标离开
			/// </summary>
			/// <remarks></remarks>
			MOUSELEAVE = 4
		}
		/// <summary>
		/// 反序列化byte数据
		/// </summary>
		/// <param name="Bin">字节型数组数据</param>
		/// <returns></returns>
		/// <remarks></remarks>
		public static object BinToCmd(byte[] Bin)
		{
			if (Bin.Length <= 0)
			{
				return null;
			}
			if (!(Bin == null))
			{
				BinaryFormatter Formatter = new BinaryFormatter(); //定义序列化对象
				MemoryStream StreamTemp = default(MemoryStream);
				object Cmd = null;
				try
				{
					StreamTemp = new MemoryStream(Bin); //数组->内存流
					Cmd = Formatter.Deserialize(StreamTemp); //内存流->对象
				}
				catch (Exception ex)
				{
					throw (new Exception(ex.Message, ex));
				}
				try
				{
					StreamTemp.Close();
					return Cmd;
				}
				catch
				{
					return null;
				}
			}
			else
			{
				return null;
			}
		}
		/// <summary>
		/// 对象、结构体转换成byte，字节型数据
		/// </summary>
		/// <param name="Cmd">对象</param>
		/// <returns></returns>
		/// <remarks></remarks>
		public static byte[] CmdToBin(object Cmd)
		{
			MemoryStream StreamTemp = new MemoryStream(); //定义流对象
			BinaryFormatter Formatter = new BinaryFormatter();
			try
			{
				Formatter.Serialize(StreamTemp, Cmd); //对象->内存流
				StreamTemp.Close();
				return StreamTemp.GetBuffer();
			}
			catch (Exception ex) //内存流->数组
			{
				throw (new Exception(ex.Message));
			}
		}
		public static string GetIniValue(string fpName, string Node, string mFileName)
		{
			string Str = "";
			string lpDefault = "";
			Str = Strings.LSet(Str, 256);
			GetPrivateProfileString(fpName, Node, lpDefault, Str, Str.Length, mFileName);
			return Microsoft.VisualBasic.Strings.Left(Str, Str.IndexOf(('\0').ToString()) + 0);
		}
		public static int WriteIniValue(string fpName, string Node, string NodeValue, string IniFile)
		{
			try
			{
				WritePrivateProfileString(fpName, Node, NodeValue, IniFile);
				return 1;
			}
			catch
			{
				return -1;
			}
		}
		public static string getNoParentPath(string mFileName, string mParentPath)
		{
			string Tmp = "";
			int a = mParentPath.Length;
			if (Strings.InStrRev(mParentPath, "\\", -1, (Microsoft.VisualBasic.CompareMethod) 0) < mParentPath.Length - 1)
			{
				mParentPath = mParentPath + "\\";
			}
			a = mParentPath.Length;
			Tmp = mFileName.Substring(a);
			return Tmp;
		}
		public static void DrawBP(object Forma, Bitmap bitmap, byte opacity)
		{
			if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
			{
				throw (new ApplicationException("The bitmap must be 32ppp with alpha-channel."));
			}
			IntPtr screenDc = Win32.GetDC(IntPtr.Zero);
			IntPtr memDc = Win32.CreateCompatibleDC(screenDc);
			IntPtr hBitmap = IntPtr.Zero;
			IntPtr oldBitmap = IntPtr.Zero;
			
			try
			{
				hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
				oldBitmap = Win32.SelectObject(memDc, hBitmap);
				Win32.Size size = new Win32.Size(bitmap.Width, bitmap.Height);
				Win32.Point pointSource = new Win32.Point(0, 0);
				Win32.Point topPos = new Win32.Point(Forma.Left, Forma.Top);
				Win32.BLENDFUNCTION blend = new Win32.BLENDFUNCTION();
				blend.BlendOp = Win32.AC_SRC_OVER;
				blend.BlendFlags = (byte) 0;
				blend.SourceConstantAlpha = opacity;
				blend.AlphaFormat = Win32.AC_SRC_ALPHA;
				
				Win32.UpdateLayeredWindow(Forma.Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.ULW_ALPHA);
			}
			finally
			{
				Win32.ReleaseDC(IntPtr.Zero, screenDc);
				if (hBitmap != IntPtr.Zero)
				{
					Win32.SelectObject(memDc, oldBitmap);
					Win32.DeleteObject(hBitmap);
				}
				Win32.DeleteDC(memDc);
			}
		}
		public class Win32
		{
			public enum Bool
			{
				@False = 0,
				@True
			}
			// 本源码来自：烈火下载 http://down.liehuo.net
			
			[StructLayout(LayoutKind.Sequential)]public struct Point
			{
				public int x;
				public int y;
				
				public Point(int x, int y)
				{
					this.x = x;
					this.y = y;
				}
			}
			
			
			[StructLayout(LayoutKind.Sequential)]public struct Size
			{
				public int cx;
				public int cy;
				
				public Size(int cx, int cy)
				{
					this.cx = cx;
					this.cy = cy;
				}
			}
			
			
			[StructLayout(LayoutKind.Sequential, Pack = 1)]private struct ARGB
			{
				public byte Blue;
				public byte Green;
				public byte Red;
				public byte Alpha;
			}
			
			
			[StructLayout(LayoutKind.Sequential, Pack = 1)]public struct BLENDFUNCTION
			{
				public byte BlendOp;
				public byte BlendFlags;
				public byte SourceConstantAlpha;
				public byte AlphaFormat;
			}
			
			
			public const int ULW_COLORKEY = 0x1;
			public const int ULW_ALPHA = 0x2;
			public const int ULW_OPAQUE = 0x4;
			
			public const byte AC_SRC_OVER = 0x0;
			public const byte AC_SRC_ALPHA = 0x1;
			
			
			[DllImport("user32.dll", ExactSpelling=true, CharSet=CharSet.Auto, SetLastError=true)]
			public static extern @Bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc, int crKey, ref BLENDFUNCTION pblend, int dwFlags);
			
			[DllImport("user32.dll", ExactSpelling=true, CharSet=CharSet.Auto, SetLastError=true)]
			public static extern IntPtr GetDC(IntPtr hWnd);
			
			[DllImport("user32.dll", ExactSpelling = true)]public static  extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
			
			[DllImport("gdi32.dll", ExactSpelling=true, CharSet=CharSet.Auto, SetLastError=true)]
			public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
			
			[DllImport("gdi32.dll", ExactSpelling=true, CharSet=CharSet.Auto, SetLastError=true)]
			public static extern @Bool DeleteDC(IntPtr hdc);
			
			[DllImport("gdi32.dll", ExactSpelling = true)]public static  extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
			
			[DllImport("gdi32.dll", ExactSpelling=true, CharSet=CharSet.Auto, SetLastError=true)]
			public static extern @Bool DeleteObject(IntPtr hObject);
		}
	}
}
